﻿/* 所有项目源码托管地址: http://panzer.codeplex.com/.
 * Visual Studio 库地址: http://visualstudiogallery.msdn.microsoft.com/b7455318-48ce-4d09-8521-a549ffd08769.
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using zoyobar.shared.panzer.ui;
using zoyobar.shared.panzer.ui.dwindow;

namespace DWCoreFAPTemplate
{

	public partial class FormTemplate : Form, ITemplateWindow
	{

		public FormTemplate ( )
		{
			InitializeComponent ( );

			// TODO: 在此处声明相关的 DataWindowCore，这里声明了 TemplateWindowCore，第一个参数是 FormTemplate 自身，第二个参数为相关的 TableSetting。
			TemplateWindowCore core = new TemplateWindowCore ( this,
				new TableSetting<PagerSetting>[] {
					new TableSetting<PagerSetting> ( "Custom", new DataTable ( ), null, null,new PagerSetting ( 1, 5 ) )
				}
				);

			// TODO: 在此处绑定数据和分页操作到相关按钮。
			core.BindPagerEvent<EventArgs> ( this.cmdSearch, "Click", EventHandlerType.EventHandler, "Custom", PagerActionType.First );
			core.BindDataEvent<EventArgs> ( this.cmdSearch, "Click", EventHandlerType.EventHandler, DataActionType.Fill, "Custom" );

			core.BindPagerEvent<EventArgs> ( this.cmdFirstPage, "Click", EventHandlerType.EventHandler, "Custom", PagerActionType.First );
			core.BindDataEvent<EventArgs> ( this.cmdFirstPage, "Click", EventHandlerType.EventHandler, DataActionType.Fill, "Custom" );

			core.BindPagerEvent<EventArgs> ( this.cmdPrevPage, "Click", EventHandlerType.EventHandler, "Custom", PagerActionType.Prev );
			core.BindDataEvent<EventArgs> ( this.cmdPrevPage, "Click", EventHandlerType.EventHandler, DataActionType.Fill, "Custom" );

			core.BindPagerEvent<EventArgs> ( this.cmdNextPage, "Click", EventHandlerType.EventHandler, "Custom", PagerActionType.Next );
			core.BindDataEvent<EventArgs> ( this.cmdNextPage, "Click", EventHandlerType.EventHandler, DataActionType.Fill, "Custom" );

			core.BindPagerEvent<EventArgs> ( this.cmdLastPage, "Click", EventHandlerType.EventHandler, "Custom", PagerActionType.Last );
			core.BindDataEvent<EventArgs> ( this.cmdLastPage, "Click", EventHandlerType.EventHandler, DataActionType.Fill, "Custom" );

			core.BindPagerEvent<EventArgs> ( this.cmdGotoPage, "Click", EventHandlerType.EventHandler, "Custom", PagerActionType.GoTo );
			core.BindDataEvent<EventArgs> ( this.cmdGotoPage, "Click", EventHandlerType.EventHandler, DataActionType.Fill, "Custom" );
		}

		public string GetCustomFullName ( )
		{ return this.txtFullName.Text.Trim ( ); }

		public void SetTables ( SortedList<string, TableSetting<PagerSetting>> tableSettings )
		{
			// TODO: 在此处将 DataTable 设置为控件的数据源，使用 tableSettings["<TableSetting 名称>"].Table 获取对应的 DataTable。
			this.gridCustom.DataSource = tableSettings["Custom"].Table;
		}

		public string GetViewFilter ( TableSetting<PagerSetting> tableSetting )
		{
			// TODO: 在此处返回 DataTable 的过滤条件，如果 DataTable 中含有 IsSelected 的 bool 类型的列，可以写："IsSelected = true"。
			return string.Empty;
		}

		public string GetViewSort ( TableSetting<PagerSetting> tableSetting )
		{
			// TODO: 在此处返回 DataTable 的排序条件，如果 DataTable 中含有 Name 的列，可以写："Name asc"。
			return string.Empty;
		}

		public int GetPagerIndex ( TableSetting<PagerSetting> tableSetting )
		{
			// TODO: 在此处返回数据分页当前页的索引，用于转到某页的操作。

			try
			{ return Convert.ToInt32 ( this.txtPageIndex.Text ); }
			catch
			{ return 0; }

		}

		public void SetIsDataActionEnabled ( TableSetting<PagerSetting> tableSetting, DataActionType actionType, bool isEnabled )
		{
			// TODO: 在此处设置更新，下载等数据操作控件的可用性，可以使用 tableSetting.Name 得到 TableSetting 的名称，actionType 说明数据操作类型，isEnabled 说明控件是否应该可用。
		}

		public void SetIsPagerControlEnabled ( TableSetting<PagerSetting> tableSetting, PagerActionType actionType, bool isEnabled )
		{
			// TODO: 在此处设置分页操作控件的可用性，可以使用 tableSetting.Name 得到 TableSetting 的名称，actionType 说明分页操作类型，isEnabled 说明控件是否应该可用。

			switch ( actionType )
			{
				case PagerActionType.First:
					this.cmdFirstPage.Enabled = isEnabled;
					break;

				case PagerActionType.Prev:
					this.cmdPrevPage.Enabled = isEnabled;
					break;

				case PagerActionType.Next:
					this.cmdNextPage.Enabled = isEnabled;
					break;

				case PagerActionType.Last:
					this.cmdLastPage.Enabled = isEnabled;
					break;

				case PagerActionType.GoTo:
					this.cmdGotoPage.Enabled = isEnabled;
					this.txtPageIndex.Enabled = isEnabled;
					break;
			}

		}

		public object Platform
		{
			get { return this; }
		}

		public void SetPager ( TableSetting<PagerSetting> tableSetting )
		{
			// TODO: 在此处将分页信息显示在窗口上。
			this.lblPagerInfo.Text = string.Format ( "第({0}/{1})页, 共{2}条", tableSetting.PagerSetting.Index, tableSetting.PagerSetting.Count, tableSetting.PagerSetting.ItemCount );
		}

	}

}
